#include <QDebug>
#include "concretemapinfo.h"
#include "ae/aeampspectrum.h"
#include "ae/aephasespectrum.h"
#include "ae/aepulsespectrum.h"
#include "ae/aewavespectrum.h"
#include "tabframe/mapinfo/ae/aeamp/aeampinfotab.h"
#include "tabframe/mapinfo/ae/aephase/aephaseinfotab.h"
#include "tabframe/mapinfo/ae/aepulse/aepulseinfotab.h"
#include "tabframe/mapinfo/ae/aewave/aewaveinfotab.h"
#include "tabframe/mapinfo/infrared/infraredinfotab.h"
#include "tabframe/mapinfo/prpd/prpdinfotab.h"
#include "tabframe/mapinfo/prps/prpsinfotab.h"
#include "tabframe/mapinfo/tev/tevamp/tevampinfotab.h"

ConcreteMapInfo::ConcreteMapInfo()
{
    // 注册各种图谱类型的创建函数
    
    // 注册AE图谱
    registerCreator<AEAmpInfoTab>(
        DataSpecificationGW::SPECTRUM_CODE_AE_AMP, 
        [](DataSpecificationGW::Spectrum* spectrum) -> AEAmpInfoTab* {
            AEAmpInfoTab* tab = new AEAmpInfoTab();
            if (!tab || !spectrum) return tab;
            
            // 转换为具体的图谱类型
            DataSpecificationGW::AEAmpSpectrum* pAEAmpSpectrum = 
                dynamic_cast<DataSpecificationGW::AEAmpSpectrum*>(spectrum);
            if (!pAEAmpSpectrum) {
                delete tab;
                return nullptr;
            }
            
            // 获取数据
            DataSpecificationGW::SpectrumHead stSpectrumHead;
            pAEAmpSpectrum->getSpectrumHead(stSpectrumHead);
            
            // 设置基本信息
            tab->setBasicInfo(stSpectrumHead);
            
            // 对每个特性组件调用loadFromSpectrum方法
            // 这将自动加载所有特性相关的数据
            for (const auto& feature : tab->getFeatures()) {
                if (feature) {
                    feature->loadFromSpectrum(spectrum);
                }
            }
            
            return tab;
        }
    );
    
    // 注册AE相位图谱
    registerCreator<AEPhaseInfoTab>(
        DataSpecificationGW::SPECTRUM_CODE_AE_PHASE, 
        [](DataSpecificationGW::Spectrum* spectrum) -> AEPhaseInfoTab* {
            AEPhaseInfoTab* tab = new AEPhaseInfoTab();
            if (!tab || !spectrum) return tab;
            
            // 转换为具体的图谱类型
            DataSpecificationGW::AEPhaseSpectrum* pAEPhaseSpectrum = 
                dynamic_cast<DataSpecificationGW::AEPhaseSpectrum*>(spectrum);
            if (!pAEPhaseSpectrum) {
                delete tab;
                return nullptr;
            }
            
            // 获取数据
            DataSpecificationGW::SpectrumHead stSpectrumHead;
            pAEPhaseSpectrum->getSpectrumHead(stSpectrumHead);
            
            // 设置基本信息
            tab->setBasicInfo(stSpectrumHead);
            
            // 对每个特性组件调用loadFromSpectrum方法
            for (const auto& feature : tab->getFeatures()) {
                if (feature) {
                    feature->loadFromSpectrum(spectrum);
                }
            }
            
            return tab;
        }
    );
    
    // 注册AE脉冲图谱
    registerCreator<AEPulseInfoTab>(
        DataSpecificationGW::SPECTRUM_CODE_AE_PULSE, 
        [](DataSpecificationGW::Spectrum* spectrum) -> AEPulseInfoTab* {
            AEPulseInfoTab* tab = new AEPulseInfoTab();
            if (!tab || !spectrum) return tab;
            
            // 获取数据
            DataSpecificationGW::SpectrumHead stSpectrumHead;
            spectrum->getSpectrumHead(stSpectrumHead);
            
            // 设置基本信息
            tab->setBasicInfo(stSpectrumHead);
            
            // 对每个特性组件调用loadFromSpectrum方法
            for (const auto& feature : tab->getFeatures()) {
                if (feature) {
                    feature->loadFromSpectrum(spectrum);
                }
            }
            
            return tab;
        }
    );
    
    // 注册AE波形图谱
    registerCreator<AEWaveInfoTab>(
        DataSpecificationGW::SPECTRUM_CODE_AE_WAVE, 
        [](DataSpecificationGW::Spectrum* spectrum) -> AEWaveInfoTab* {
            AEWaveInfoTab* tab = new AEWaveInfoTab();
            if (!tab || !spectrum) return tab;
            
            // 获取数据
            DataSpecificationGW::SpectrumHead stSpectrumHead;
            spectrum->getSpectrumHead(stSpectrumHead);
            
            // 设置基本信息
            tab->setBasicInfo(stSpectrumHead);
            
            // 对每个特性组件调用loadFromSpectrum方法
            for (const auto& feature : tab->getFeatures()) {
                if (feature) {
                    feature->loadFromSpectrum(spectrum);
                }
            }
            
            return tab;
        }
    );
    
    // 注册PRPD图谱
    registerCreator<PRPDInfoTab>(
        DataSpecificationGW::SPECTRUM_CODE_HFCT_PRPD, 
        [](DataSpecificationGW::Spectrum* spectrum) -> PRPDInfoTab* {
            PRPDInfoTab* tab = new PRPDInfoTab();
            if (!tab || !spectrum) return tab;
            
            // 获取数据
            DataSpecificationGW::SpectrumHead stSpectrumHead;
            spectrum->getSpectrumHead(stSpectrumHead);
            
            // 设置基本信息
            tab->setBasicInfo(stSpectrumHead);
            
            // 对每个特性组件调用loadFromSpectrum方法
            for (const auto& feature : tab->getFeatures()) {
                if (feature) {
                    feature->loadFromSpectrum(spectrum);
                }
            }
            
            return tab;
        }
    );
    
    // 同样处理UHF PRPD
    registerCreator<PRPDInfoTab>(
        DataSpecificationGW::SPECTRUM_CODE_UHF_PRPD, 
        [](DataSpecificationGW::Spectrum* spectrum) -> PRPDInfoTab* {
            PRPDInfoTab* tab = new PRPDInfoTab();
            if (!tab || !spectrum) return tab;
            
            // 获取数据
            DataSpecificationGW::SpectrumHead stSpectrumHead;
            spectrum->getSpectrumHead(stSpectrumHead);
            
            // 设置基本信息
            tab->setBasicInfo(stSpectrumHead);
            
            // 对每个特性组件调用loadFromSpectrum方法
            for (const auto& feature : tab->getFeatures()) {
                if (feature) {
                    feature->loadFromSpectrum(spectrum);
                }
            }
            
            return tab;
        }
    );
    
    // 注册PRPS图谱
    registerCreator<PRPSInfoTab>(
        DataSpecificationGW::SPECTRUM_CODE_HFCT_PRPS, 
        [](DataSpecificationGW::Spectrum* spectrum) -> PRPSInfoTab* {
            PRPSInfoTab* tab = new PRPSInfoTab();
            if (!tab || !spectrum) return tab;
            
            // 获取数据
            DataSpecificationGW::SpectrumHead stSpectrumHead;
            spectrum->getSpectrumHead(stSpectrumHead);
            
            // 设置基本信息
            tab->setBasicInfo(stSpectrumHead);
            
            // 对每个特性组件调用loadFromSpectrum方法
            for (const auto& feature : tab->getFeatures()) {
                if (feature) {
                    feature->loadFromSpectrum(spectrum);
                }
            }
            
            return tab;
        }
    );
    
    // 同样处理UHF PRPS
    registerCreator<PRPSInfoTab>(
        DataSpecificationGW::SPECTRUM_CODE_UHF_PRPS, 
        [](DataSpecificationGW::Spectrum* spectrum) -> PRPSInfoTab* {
            PRPSInfoTab* tab = new PRPSInfoTab();
            if (!tab || !spectrum) return tab;
            
            // 获取数据
            DataSpecificationGW::SpectrumHead stSpectrumHead;
            spectrum->getSpectrumHead(stSpectrumHead);
            
            // 设置基本信息
            tab->setBasicInfo(stSpectrumHead);
            
            // 对每个特性组件调用loadFromSpectrum方法
            for (const auto& feature : tab->getFeatures()) {
                if (feature) {
                    feature->loadFromSpectrum(spectrum);
                }
            }
            
            return tab;
        }
    );
    
    // 注册TEV幅值图谱
    registerCreator<TEVAmpInfoTab>(
        DataSpecificationGW::SPECTRUM_CODE_TEV_AMP, 
        [](DataSpecificationGW::Spectrum* spectrum) -> TEVAmpInfoTab* {
            TEVAmpInfoTab* tab = new TEVAmpInfoTab();
            if (!tab || !spectrum) return tab;
            
            // 获取数据
            DataSpecificationGW::SpectrumHead stSpectrumHead;
            spectrum->getSpectrumHead(stSpectrumHead);
            
            // 设置基本信息
            tab->setBasicInfo(stSpectrumHead);
            
            // 对每个特性组件调用loadFromSpectrum方法
            for (const auto& feature : tab->getFeatures()) {
                if (feature) {
                    feature->loadFromSpectrum(spectrum);
                }
            }
            
            return tab;
        }
    );
    
    // 注册红外图谱
    registerCreator<InfraredInfoTab>(
        DataSpecificationGW::SPECTRUM_CODE_INFRARED, 
        [](DataSpecificationGW::Spectrum* spectrum) -> InfraredInfoTab* {
            InfraredInfoTab* tab = new InfraredInfoTab();
            if (!tab || !spectrum) return tab;
            
            // 获取数据
            DataSpecificationGW::SpectrumHead stSpectrumHead;
            spectrum->getSpectrumHead(stSpectrumHead);
            
            // 设置基本信息
            tab->setBasicInfo(stSpectrumHead);
            
            // 对每个特性组件调用loadFromSpectrum方法
            for (const auto& feature : tab->getFeatures()) {
                if (feature) {
                    feature->loadFromSpectrum(spectrum);
                }
            }
            
            return tab;
        }
    );
    
    // 可以继续注册其他类型的图谱...
}

MapInfoTab* ConcreteMapInfo::createMapInfoTab(DataSpecificationGW::Spectrum* spectrum)
{
    if (!spectrum) {
        qWarning() << "Null spectrum pointer";
        return nullptr;
    }
    
    DataSpecificationGW::SpectrumHead stSpectrumHead;
    spectrum->getSpectrumHead(stSpectrumHead);
    uint8_t typeCode = stSpectrumHead.eSpectrumTypeCode;
    
    // 查找对应的创建函数
    auto it = m_creators.find(typeCode);
    if (it != m_creators.end()) {
        // 调用创建函数
        return it->second(spectrum);
    }
    
    qWarning() << "Unsupported spectrum type code:" << typeCode;
    return nullptr;
}

bool ConcreteMapInfo::supportsSpectrumType(uint8_t spectrumTypeCode) const
{
    return m_creators.find(spectrumTypeCode) != m_creators.end();
}
